package com.cn.qgx428.lazydiary.anim.viewPager

import android.support.v4.view.ViewPager
import android.view.View

/**
 * Created by QinX on 2017/12/15.
 */
class ZoomOutPageTransformer : ViewPager.PageTransformer {

    companion object {
        val MIN_SCALE = 0.85F
        val MIN_ALPHA = 0.5F
    }

    override fun transformPage(page: View, position: Float) {
        val pageWidth = page?.width
        val pageHeight = page?.height

        if (position<-1){
            page?.setAlpha(0F)
        } else if(position<=1){
            val scaleFactor = Math.max(MIN_SCALE, 1-Math.abs(position))
            val vertMargin = pageHeight!! * (1 - scaleFactor) / 2
            val  horzMargin = pageWidth!! * (1 - scaleFactor) / 2
            if (position<0){
                page.translationX = horzMargin - vertMargin / 2
            } else{
                page.translationX = -horzMargin + vertMargin / 2
            }

            page.scaleX = scaleFactor
            page.scaleY = scaleFactor
            page.alpha = MIN_ALPHA + (scaleFactor - MIN_SCALE)/ (1 - MIN_SCALE) * (1 - MIN_ALPHA)
        } else{
            page?.alpha = 0F
        }
    }
}